<template>
	<view class="container">
		<!-- s收货地址 -->
		<view class="address">
			<view class="address_info">
				<label for="">收件人:{{ information.name }}</label>
				<label for="">{{ information.phone }}</label>
			</view>
			<view class="address_res">
				收货地址：<label for="">{{ information.address }}</label>
			</view>
		</view>

		<!-- 商品信息 -->
		<view class="carts">
			<!-- cartsInfo 循环体 -->
			<view class="cartsInfo" v-for="(item,index) of carts" :key="item.id">
				<!-- 70px -->
				<view class="carts_detail">
					<view class="carts_image">
						<image :src="item.img" mode=""></image>
					</view>
					<view class="carts_name">
						<view for="" style="font-size: 30rpx;">{{item.goodsname}}</view>
						<view for="" style="font-size: 26rpx; color: #C0C0C0;">规格:白色</view>
					</view>
					<view class="carts_price">
						<label for="">￥{{item.price}}</label>
					</view>
				</view>
				<!-- 50px -->
				<view class="cart_num">
					<view class="">
						购买数量：
					</view>
					<view class="">
						<label for="" class="jian">-</label>
						<label for="" class="num">{{item.num}}</label>
						<label for="" class="jia">+</label>
					</view>
				</view>
				<!-- 横线 -->
				<view class="xian"></view>
			</view>
			<!-- 50px -->
			<view class="kuaidi">
				<label for="">配送方式</label>
				<label for="">XX快递</label>
			</view>
		</view>

		<!-- 优惠券 -->
		<view class="yhquan">
			<label for="">优惠券</label>
			<label for="" style="margin-right: 40rpx;">无可用</label>
		</view>
		<!-- jifen积分 -->
		<view class="jifen">
			<view class="use">
				<label for="">使用积分</label>
			</view>
			<view class="input" style="margin-right: 40rpx;">
				<input type="text" value="" placeholder="输入积分" />
				<label for="" class="input_use">使用</label>
				<label for="" class="input_keyi">可使用50积分</label>
			</view>
		</view>

		<!-- 具体详细 -->
		<view class="resDetail">
			<view class="">
				<label for="">商品金额</label>
				<label for="" style="margin-right:40rpx;color:red">￥{{_totalPrice}}</label>
			</view>
			<view class="">
				<label for="">运费</label>
				<label for="" style="margin-right:40rpx;color:red">+￥0.00</label>
			</view>
			<view class="">
				<label for="">优惠券</label>
				<label for="" style="margin-right:40rpx;color:red">-￥0.00</label>
			</view>
			<view class="">
				<label for="">会员优惠</label>
				<label for="" style="margin-right:40rpx;color:red">-￥0.00</label>
			</view>
			<view class="">
				<label for="">积分抵扣</label>
				<label for="" style="margin-right:40rpx;color:red">-￥0.00</label>
			</view>
		</view>
		<!-- 实付金额 -->
		<view class="sfje">
			实付金额：<label for="" style="color:red">￥{{confirmPrice}}</label>
		</view>

		<!-- 确认订单按钮 -->
		<view class="btn">
			<!-- 点击提交订单按钮，发起支付请求 -->
			<button type="primary" @click="_addOrder">提交订单</button>
		</view>
	</view>
</template>

<script>
	import Http from '@/utils/http.js'
	let appObj = getApp()
	export default {
		data() {
			return {
				information: {
					name: 'YouSu',
					phone: '15811112222',
					address: '北京市海淀区隐泉路清林苑6号楼中公优就业总部3层'
				},
				carts:[],//购物车中的数据
				yhq:-1000
			}
		},
		mounted(){
			this._getCarts()
		},
		computed:{
			confirmPrice(){
				return this._totalPrice + this.yhq
			},
			_totalPrice(){
				let totalPrice = 0
				this.carts.forEach(item=>{
					totalPrice += item.num * item.price
				})
				return totalPrice
			},
			_totalNum(){
				let num = 0
				this.carts.forEach(item=>{
					num += item.num 
				})
				return num
			}
		},
		methods: {
			
			/*
			uid会员id
			username收货人姓名
			userphone收货人联系方式
			address收货人地址
			countmoney订单总支付金额
			countnumber订单商品数量
			addtime订单添加时间（时间戳）
			idstr购物车数据id字符串 例如：idstr = "1,2,3"
			authorization  header头中需要添加token后台验证条件
			*/
			//提交订单
			async _addOrder(){
				let uid = appObj.userInfo.uid
				let authorization = appObj.userInfo.token
				let username = this.information.name 
				let userphone = this.information.phone
				let address = this.information.address
				let countmoney = this.confirmPrice
				let countnumber = this._totalNum
				let addtime = new Date().getTime()
				
				let idstr = ''
				this.carts.forEach(item=>{
					idstr+=item.id + ','
				})
				
				idstr = idstr.substring(0,idstr.length-1)
				//console.log( idstr )
				let params = {
					uid,username,userphone,address,countmoney,countnumber,addtime
				}
				
				params = JSON.stringify(params)
				
				
				let result = await Http({
						url:'/api/orderadd',
						data:{
							params,
							idstr
							
						},
						header:{
							authorization
						}
					})
					if( result.data.msg == "提交订单成功" ){
						uni.redirectTo({
							url:"../order/order"
						})
					}
				console.log( result,'add result')
			},
			_getCarts(){
					let carts = uni.getStorageSync('carts')
					console.log( carts )
					this.carts = carts
			},
			
			_getYouHui(){
				
			},
			_addAddress(){
				//   /api/addressadd
					/*
						name
						tel
						address
					*/
			}
		}
	}
</script>

<style>
	@import url("../../common/css/confirm.css");
</style>

<!-- 

 有关订单提交的后续 
 1. 点击提交订单（支付）
	1.1如果没支付（也会生成订单，只不过订单的状态是未支付）
	status：1 2 3 4 5 6 7 8 9 
	1.2点击支付，选择一些支付方式，最后支付，那么大家觉得订单生成会存储一些什么数据到数据库表中。
	payment:1 2 3
2. 支付大家觉得需要传什么参数。
	paytable 表
	/pay
	{
		orderid:
		money
	}
	支付时间  						支付的订单  			为哪个商品的订单号      总金额
	2020-9-1 9：35 			202009010935123423234 				83             12298
 -->